﻿using System;
using System.IO;
using MingYuanyun.WebApi.Abstractions;
using MingYuanYun.WebApi.Logging;
using MingYuanYun.WebApi.Cors;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ControlCenter.ApplicationService;
using ControlCenter.ApplicationService.Jobs;
using ControlCenter.Data;
using Hangfire;
using Hangfire.MemoryStorage;
using Microsoft.Extensions.FileProviders;
using MingYuanYun.Context;

namespace ControlCenterService.WebApi
{
    /// <summary>
    /// 
    /// </summary>
    public class Startup
    {

        /// <summary>
        /// 
        /// </summary>
        public IConfiguration Configuration { get; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }


        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRouting(opt => opt.LowercaseUrls = true);
            
            services.AddMvc(opt =>
            {
                opt.AddApiVersionRoutePrefixConvention("api/v1/controlcenterService");
            }).AddJsonDateFormat("yyyy-MM-dd HH:mm:ss");

            services.AddSwagger();

            services.AddCors(Configuration.GetSection("CorsOptions").Get<CorsOptions>());

            services.AddHttpContextAccessor();

            services.AddStructuredNLog(Configuration);
            
            services.AddMemoryCache();

            services.AddApplicationServices(Configuration);

            ServiceLocator.SetServices(services.BuildServiceProvider());
            services.AddHangfire(x => x.UseStorage(new MemoryStorage()));
        }

        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
            defaultFilesOptions.DefaultFileNames.Clear();
            defaultFilesOptions.DefaultFileNames.Add("index.html");
            app.UseDefaultFiles(defaultFilesOptions);
            app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "upload")),
                RequestPath = "/upload"
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors(CorsOptions.PolicyName);

            app.UseHealthCheck();

            app.UseSwagger();

            app.UseMiddleware<ErrorHandlingMiddleware>();

            app.UseHangfireServer();
            app.UseHangfireDashboard();
            RecurringJob.AddOrUpdate<IRobotStatusUpdateJob>(s => s.Run(), "0/5 * * * * *");

            app.UseMvc();
        }
    }
}
